local base = _G
module("gui")

WindowG = {}
WindowGMT = { __index = WindowG }

base.setmetatable(WindowG, { __index = MContainerG })

WindowG.create = function(x, y, width, height, mt)
  local colors = get_colors('background', 'border', 'gradient')

  local self = MContainer(x, y, width, height, mt or WindowGMT)

  self.colors = colors

  return self
end

WindowG.destroy = function(self)
  self.colors = nil
  MContainerG.destroy(self)
end

WindowG.redraw = function(self, delta)
  MContainerG.redraw(self, delta)

  local alpha = 1--[[0.5
  if self.is_focused == true then
    alpha = 1
  elseif self.is_hovered == true then
    alpha = 0.75
  end
  ]]
  local c = self.sprite.canvas
  c:clear()

  -- draw background rectangle
  local l, t, r, b = 0, -self.height, self.width, 0
  if self.colors.border then
    c:rect(l - 1, t - 1, r + 1, b + 1)
    c:set_fill_style(self.colors.border, alpha)
    c:fill()
  end
  if self.colors.background then
    c:rect(l, t, r, b)
    if self.colors.gradient then
      local grad = base.LinearGradient()
      grad:stop_point1(0, -self.height, self.colors.gradient)
      grad:stop_point2(0, 0, self.colors.background)
      c:rect(l, t, r, b)
      c:set_fill_style(grad, alpha)
      c:fill_gradient()
    end
    c:set_fill_style(self.colors.background, 1)
    c:fill()
  end
  -- todo clipping
end

WindowG.update = function(self, dt)
  MContainerG.update(self, dt)
  self:redraw()
end

Window = WindowG.create